<%#
kind: snippet
name: redhat_register
model: ProvisioningTemplate
snippet: true
description: |
  Red Hat Registration Snippet

  General parameters:

    redhat_install_host_tools = [true|false]    Install the katello-host-tools yum/dnf plugins.

    redhat_install_host_tracer_tools = [true|false]  Install the katello-host-tools Tracer yum/dnf plugin.

    activation_key = <key>                      Activation key string, not needed if using
                                                subscription-manager with username/password

  Parameters for use with subscription-manager (Red Hat CDN, Satellite, or Katello)

    subscription_manager = 'true'               You're going to use subscription-manager

    subscription_manager_auto_attach = 'false'  Run attach --auto after registering.

    subscription_manager_status = 'true'        Show status of subscription-manager

    subscription_manager_refresh = 'true'       Run refresh after registering host.

    subscription_manager_refresh_force = 'true' Add --force to `subscription_manager_refresh` command.

    subscription_manager_username = <username>  Username for subscription-manager

    subscription_manager_password = <password>  Password for subscription-manager

    subscription_manager_certpkg_url = <url>    Custom certificate package URL

    subscription_manager_org = <org name>       Organization name, if required

    subscription_manager_repos = <repos>        Additional repositories to enable
                                                after registration.
                                                Seperate multiple repositories with commas.

    subscription_manager_override_repos_cost = <cost>  Override repository cost

    subscription_manager_pool = <pool>          Specific subscription pool to use

    only_subscription_manager_repos = 'true'    dnf/yum should only use repos managed by sub-man

    http-proxy = <host>                         Proxy hostname to be used for registration

    http-proxy-port = <port>                    Proxy port to be used for registration

    http-proxy-user = <user>                    Proxy user to be used for registration

    http-proxy-password = <password>            Proxy password to be used for registration

    syspurpose_role                             Sets the system purpose role

    syspurpose_usage                            Sets the system purpose usage

    syspurpose_sla                              Sets the system purpose SLA

  Set these parameters if you're using rhnreg_ks:

    spacewalk_host = <hostname>                 Hostname of Spacewalk server
-%>
<%
  # Katello or subscription-manager:
  if host_param_true?('subscription_manager') || host_param('kt_activation_keys')
    registration_type = 'subscription_manager'
  # Spacewalk:
  elsif host_param('spacewalk_host')
    registration_type = 'spacewalk'
  else
    registration_type = nil
  end
-%>
# registration_type = '<%= registration_type %>'
<% if registration_type == 'subscription_manager' -%>
  <%
    if host_param('kt_activation_keys')
      subscription_manager_certpkg_url = subscription_manager_configuration_url(@host)
      subscription_manager_org = @host.rhsm_organization_label
      activation_key = host_param('kt_activation_keys')
      redhat_install_host_tools = host_param_true?('redhat_install_host_tools', true)
      redhat_install_host_tracer_tools = host_param_true?('redhat_install_host_tracer_tools')
    else
      subscription_manager_certpkg_url = host_param('subscription_manager_certpkg_url')
      subscription_manager_org = host_param('subscription_manager_org')
      activation_key = host_param('activation_key')
      redhat_install_host_tools = host_param_true?('redhat_install_host_tools')
      redhat_install_host_tracer_tools = host_param_true?('redhat_install_host_tracer_tools')
    end
  -%>

  echo "##############################################################"
  echo "################# SUBSCRIPTION MANAGER #######################"
  echo "##############################################################"
  echo
  echo "Starting the subscription-manager registration process"

  # Set up subscription-manager
  <%= snippet("subscription_manager_setup", variables: { subman_setup_scenario: 'provisioning' }).strip -%>

  <%- if (host_param('syspurpose_role') || host_param('syspurpose_usage') || host_param('syspurpose_sla')) -%>
    # Avoid timeout accessing unreachable repo on air gapped infrastructure,
    #  assuming subscription-manager-syspurpose is installed in custom packages section.
    if ! rpm --query --quiet subscription-manager-syspurpose ; then
      $PKG_MANAGER_INSTALL subscription-manager-syspurpose
    fi

    if [ -f /usr/sbin/syspurpose ]; then
      <%- if host_param('syspurpose_role') -%>
        syspurpose set-role "<%= host_param('syspurpose_role') %>"
      <%- end -%>
      <%- if host_param('syspurpose_usage') -%>
        syspurpose set-usage "<%= host_param('syspurpose_usage') %>"
      <%- end -%>
      <%- if host_param('syspurpose_sla') -%>
        syspurpose set-sla "<%= host_param('syspurpose_sla') %>"
      <%- end -%>
    else
      echo "Syspurpose CLI not found."
    fi
  <% end -%>

  <% if host_param('http-proxy') -%>
    subscription-manager config --server.proxy_hostname='<%= host_param("http-proxy") %>'
    <% if host_param('http-proxy-user') -%>
      subscription-manager config --server.proxy_user='<%= host_param("http-proxy-user") %>'
    <% end -%>
    <% if host_param('http-proxy-password') -%>
      subscription-manager config --server.proxy_password='<%= host_param("http-proxy-password") %>'
    <% end -%>
    <% if host_param('http-proxy-port') -%>
      subscription-manager config --server.proxy_port='<%= host_param("http-proxy-port") %>'
    <% end -%>
  <% end -%>

  <% if host_param('subscription_manager_username') && host_param('subscription_manager_password') -%>
    <% if host_param('subscription_manager_pool') -%>
      subscription-manager register --name="<%= @host.name %>" --username='<%= host_param("subscription_manager_username") %>' --password='<%= host_param("subscription_manager_password") %>'
      subscription-manager attach --pool='<%= host_param('subscription_manager_pool') %>'
    <% else -%>
      subscription-manager register --name="<%= @host.name %>" --username='<%= host_param("subscription_manager_username") %>' --password='<%= host_param("subscription_manager_password") %>' --auto-attach
    <% end -%>

  <% elsif activation_key -%>
    subscription-manager register --name="<%= @host.name %>" --org='<%= subscription_manager_org %>' --activationkey='<%= activation_key %>'
  <% else -%>
    echo "No activation key found: Not registering to subscription manager"
  <% end -%>

  <% if host_param_true?('only_subscription_manager_repos') -%>
    for subman_config_file in /etc/yum/pluginconf.d/subscription-manager.conf /etc/dnf/plugins/subscription-manager.conf; do
      if [ -f $subman_config_file ]; then
        egrep -q "^disable_system_repos=" $subman_config_file
        if [ "$?" -eq 0 ]; then
          sed s/^disable_system_repos=.*/disable_system_repos=1/ $subman_config_file >"${subman_config_file}.new"
          mv -f "${subman_config_file}.new" $subman_config_file
        else
          echo "disable_system_repos=1" >>$subman_config_file
        fi
      fi
    done
  <% end -%>

  <% if host_param_true?('subscription_manager_auto_attach', false) -%>
    subscription-manager attach --auto

  <% end -%>

  <% if host_param_true?('subscription_manager_refresh', false) -%>
  subscription-manager refresh <% if host_param_true?('subscription_manager_refresh_force', false) %> --force<% end %>
  <% end -%>

  <% if host_param_true?('subscription_manager_status', false) -%>
  subscription-manager status
  <% end -%>

  <% if host_param('subscription_manager_repos') -%>
    # workaround for RHEL 6.4 bug https://bugzilla.redhat.com/show_bug.cgi?id=1008016
    subscription-manager repos --list > /dev/null
    <%= "subscription-manager repos --enable #{host_param('subscription_manager_repos').gsub(/,\s*/, ' --enable ')}" %>
  <% end -%>

  <% if host_param('subscription_manager_override_repos_cost') -%>
    for repo in $(subscription-manager repos --list-enabled | grep "Repo ID:" | awk -F' ' '{ print $3 }'); do
      <%= "subscription-manager repo-override --list --repo $repo | grep 'cost:' &>/dev/null || subscription-manager repo-override --repo $repo --add=cost:#{host_param('subscription_manager_override_repos_cost')}" %>
    done
  <% end -%>

  <% if redhat_install_host_tools -%>
    $PKG_MANAGER_INSTALL katello-host-tools
  <% end -%>

  <% if redhat_install_host_tracer_tools -%>
    $PKG_MANAGER_INSTALL katello-host-tools-tracer
  <% end -%>
<% end -%>

<% if registration_type == 'spacewalk' -%>
  echo "##############################################################"
  echo "################ SPACEWALK REGISTRATION ######################"
  echo "##############################################################"

  <% if host_param('activation_key') -%>
    rhn_activation_key="<%= host_param('activation_key') -%>"
    satellite_hostname="<%= host_param('spacewalk_host') -%>"
    rhn_cert_file="RHN-ORG-TRUSTED-SSL-CERT"

    echo "Registering to RHN Satellite at [$satellite_hostname]"
    echo "Using Registration Key [$rhn_activation_key]"

    # Obtain our RHN Satellite Certificate
    echo "Obtaining RHN SSL certificate"
    curl -o /usr/share/rhn/$rhn_cert_file -k https://$satellite_hostname/pub/$rhn_cert_file

    <% if @host.operatingsystem.name == 'SLES' -%>
      # If SLES then add CA Cert to CA Certs for curl
      cp /usr/share/rhn/$rhn_cert_file /etc/ssl/certs/
      ln -s /etc/ssl/certs/$rhn_cert_file /etc/ssl/certs/`openssl x509 -hash -noout -in /etc/ssl/certs/$rhn_cert_file`.0
    <% end -%>

    # Update our up2date configuration file
    echo "Updating SSL CA Certificate to /usr/share/rhn/$rhn_cert_file"
    sed -i -e "s|^sslCACert=.*$|sslCACert=/usr/share/rhn/$rhn_cert_file|" /etc/sysconfig/rhn/up2date

    # Update our Satellite Hostname
    echo "Updating Satellite Hostname to [$satellite_hostname]"
    sed -i -e "s|^serverURL=.*$|serverURL=https://$satellite_hostname/XMLRPC|" /etc/sysconfig/rhn/up2date
    sed -i -e "s|^noSSLServerURL=.*$|noSSLServerURL=https://$satellite_hostname/XMLRPC|" /etc/sysconfig/rhn/up2date

    # Restart messagebus/HAL to try and prevent hardware detection errors in rhnreg_ks
    echo "Restarting services..."
    <% if @host.operatingsystem.name == 'SLES' && @host.operatingsystem.major.to_i < 12 -%>
      service haldaemon restart
    <% else -%>
      service messagebus restart
      service hald restart
    <% end -%>

    # Now, perform our registration
    #  (might get hardware errors here, due to dbus/messagebus lameness. These are safe to ignore.)
    echo -n "Performing RHN Registration... "
    rhnreg_ks --activationkey=$rhn_activation_key
    echo "done."

    # Check we registered
    echo -n "Checking System Registration... "
    if ! rhn_check; then
        echo "FAILED"
        echo " >> RHN Registration FAILED. Please Investigate. <<"
    else
        echo "registration successful."
    fi

  <% else -%>
    echo "No activation key found: Not registering"
  <% end -%>
<% end -%>
